﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xinkui.Entity.User;
using CommonFramework.Utility;
using CommonFramework.Storage.Cookie;
using Xinkui.Common;
using Xinkui.DataAccess.User;
using System.Web;
using Xinkui.DataAccess.AutoGenerator;

namespace Xinkui.WebModel {
    public class UserManager {
        public static LoginUserEntity GetCurLoginUser() {
            LoginUserEntity r = null;
            if ((r = HttpContext.Current.Session[SystemConst.CookieConst.LoginUser] as LoginUserEntity) != null) return r;
            int userID = CookieManager.Get(SystemConst.CookieConst.LoginUser).ToType<int>(0);
            UserMock.MockUserId(ref userID);
            if (userID > 0) {

                LoginUserEntity l = UserDataAccess.UserLogin(userID);
                l.LoginDate = DateTime.Now;
                HttpContext.Current.Session[SystemConst.CookieConst.LoginUser] = l;
            }
            return null;
        }


        public static LoginUserEntity Login(string username, string pwd) {
            LoginUserEntity loginUser = UserDataAccess.UserLogin(username, pwd);
            if (loginUser != null && loginUser.CompanyID > 0) {
                CookieManager.Set(SystemConst.CookieConst.LoginUser, loginUser.UserID.ToString());
                HttpContext.Current.Session[SystemConst.CookieConst.LoginUser] = loginUser;
            }
            return loginUser;
        }

        public static void Logout() {
            CookieManager.Remove(SystemConst.CookieConst.LoginUser);
            HttpContext.Current.Session.RemoveAll();
        }

    }

    public class UserMock {

        public static void MockUserId(ref int userId) {
            userId = userId;
           return ;
            if (userId <= 0) {
                userId = 3;
            }
        }
    }
}
